<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>操作日志表</title>
		<link rel="stylesheet" href="js/bower_components/bootstrap/dist/css/bootstrap.min.css">
		<link rel="stylesheet" href="js/bower_components/bootstrap-select/dist/css/bootstrap-select.min.css">
		<link rel="stylesheet" href="js/bower_components/bootstrap-switch/dist/css/bootstrap3/bootstrap-switch.min.css">
		<link rel="stylesheet" href="js/bower_components/bootstrap-table/dist/bootstrap-table.min.css">
		<link rel="stylesheet" href="js/bower_components/bootstrap-treeview/dist/bootstrap-treeview.min.css">
		<link href="js/bower_components/bootstrap-datetimepicker-master/css/bootstrap-datetimepicker.min.css"
			rel="stylesheet">

		<script src="js/bower_components/jquery/dist/jquery.min.js"></script>
		<script src="js/bower_components/bootstrap/dist/js/bootstrap.min.js"></script>
		<script src="js/bower_components/bootbox.min.js"></script>
		<script src="js/bower_components/bootstrap-select/js/bootstrap-select.js"></script>
		<script src="js/bower_components/bootstrap-switch/dist/js/bootstrap-switch.min.js"></script>
		<script src="js/bower_components/bootstrap-table/dist/bootstrap-table.min.js"></script>
		<script src="js/bower_components/bootstrap-table/dist/locale/bootstrap-table-zh-CN.js"></script>

		<script src="js/bower_components/bootstrap-treeview/dist/bootstrap-treeview.min.js"></script>
		<script src="js/bower_components/jquery-form-validator/form-validator/jquery.form-validator.min.js"></script>
		<script src="js/bower_components/moment/min/moment-with-locales.min.js"></script>
		<script src="js/bower_components/bootstrap-datetimepicker-master/js/bootstrap-datetimepicker.min.js"></script>

		<script>
			/*获取列表数据*/
			function getDate() {
				MyLog = {
					"beginPage": 0,
					"pageCount": 5
				};
				$.ajax({
					type: "put",
					url: "http://localhost:2021/mylog/getAll",
					contentType: "application/json;charset=utf-8",
					data: JSON.stringify(MyLog),
					dataType: "json",
					success: function(res) {
						console.log(res);
						setTable(res)
					}
				});
			}

			/*将数据绑定在表格上*/
			function setTable(myLogList) {
				$("#tbMyLog").bootstrapTable('destroy')
				$("#tbMyLog").bootstrapTable({
					data: myLogList,
					striped: true,
					contentType: "application/x-www-form-urlencoded;charset=utf-8",
					uniqueId: "value",
					idField: 'userID',
					checkboxHeader: true,
					singleSelect: true,
					clickToSelect: true,
					columns: [{
						field: "userID",
						align: "center",
						visible: false
					}, {
						field: "IPAddress",
						title: "IP地址",
						align: "center"
					}, {
						field: "dotime",
						title: "日期区间",
						align: "center"
					}, {
						field: "doname",
						title: "操作方法名称",
						checkbox: true,
						align: "center"
					}]
				})
			}

			//获取列表中选中的数据
			function getSelectID() {
				var id = $("#tbMyLog").bootstrapTable('getSelections');
				console.log(id);
				if (id.length > 0) {
					return id[0].userID;
				} else {
					bootbox.alert("选择一条要修改的信息!")
				}
			}

			$(function() {
				//数据初始化
				getDate()
				//给搜索框注入点击事件
				$("#btnSelect").click(function() {
					var dotime = $("#txtLogname").val();
					console.log(dotime);
					if (dotime.length == 0) {
						getDate()
					} else {
						MyLog = {
							"beginPage": 0,
							"pageCount": 5,
							"dotime": dotime
						};
						$.ajax({
							type: "put",
							url: "http://localhost:2021/mylog/getAll",
							contentType: "application/json;charset=utf-8",
							data: JSON.stringify(MyLog),
							dataType: "json",
							success: function(res) {
								console.log(res);
								setTable(res)
							}
						});
					}
				})

				//新增功能
				$("#btnAdd").click(function() {
					//给模态框设置标题
					$("#myModalLabel").text("新增用户信息")
					//打开模态窗体
					$("#divMyLogForm").modal();
				})

				//给保存数据按钮添加点击事件
				$("#butSave").click(function() {
					var id = $("#userID").val();
					console.log(id);
					if (id != null && id != 0) {
						//修改信息
						MyLog = {
							"dotime": $("#dotime").val(),
							"IPAddress": $("#IPAddress").val(),
							"userID": id
						};
						$.ajax({
							type: "put",
							url: "http://localhost:2021/mylog/updatelog",
							contentType: "application/json;charset=utf-8",
							data: JSON.stringify(MyLog),
							dataType: "json",
							success: function(res) {
								console.log("修改数据后，返回结果为：")
								console.log(res);
								if (res > 0) {
									alert("Successfully save!");
									$("#divMyLogForm").modal('hide')
									getDate();
								}
							}
						});
					} else {
						//添加新信息
						MyLog = {
							"dotime": $("#dotime").val(),
							"IPAddress": $("#IPAddress").val()
						};
						$.ajax({
							type: "put",
							url: "http://localhost:2021/mylog/addLog",
							contentType: "application/json;charset=utf-8",
							data: JSON.stringify(MyLog),
							dataType: "json",
							success: function(res) {
								console.log("添加数据后，返回结果为：")
								console.log(res);
								if (res > 0) {
									alert("Successfully save!");
									$("#divMyLogForm").modal('hide')
									getDate();
								}
							}
						});

					}
				})

				//给修改按钮添加点击事件
				$("#btnUpdate").click(function() {
					//获取修改列表的主键
					var userID = getSelectID();
					console.log(userID);
					$.ajax({
						type: "get",
						url: "http://localhost:2021/mylog/getLog?userID=" + userID,
						contentType: "application/json;charset=utf-8",
						dataType: "json",
						success: function(res) {
							console.log("根据主键获取后端返回的结果为:")
							console.log(res);
							if (res != null) {
								$("#myModalLabel").text("修改一下吧");
								$("#dotime").val(res["dotime"]);
								$("#IPAddress").val(res["IPAddress"]);
								$("#userID").val(res["userID"]);
								//打开模态窗体
								$("#divMyLogForm").modal();
							}
						}
					})
				})

				//给删除按钮注入击键信息，删除列表当中的信息
				$("#btnDel").click(function() {
					console.log("进入删除的方法");
					//获取主键
					var userID = getSelectID();
					console.log(userID);
					$.ajax({
						type: "get",
						url: "http://localhost:2021/mylog/deleteLog?userID=" + userID,
						contentType: "application/json;charset=utf-8",
						dataType: "json",
						success: function(res) {
							console.log("根据主键删除数据，返回的结果为:")
							console.log(res);
							if (res == 1) {
								bootbox.alert("Successfully delete!");
								getDate();
							}
						}
					})
				})

			})
		</script>
	</head>

	<body>
		<!--查询区域-->
		<div class="input-group">
			日期查询：&nbsp;<input type="text" id="txtLogname" /> &nbsp;&nbsp;
			<button type="button" id="btnSelect" class="btn btn-default">Search</button>
		</div>

		<!--功能区域-->
		<div style="margin: 5px;margin-top: 10px;">
			<button type="button" id="btnAdd" class="btn btn-success">添加信息</button>
			<button type="button" id="btnUpdate" class="btn btn-info">修改信息</button>
			<button type="button" id="btnDel" class="btn btn-danger">删除信息</button>
		</div>

		<!--列表区域-->
		<div class="row col-md-10" style="margin-top: 10px;">
			<table id="tbMyLog"></table>
		</div>

		<div class="modal fade" id="divMyLogForm" tabindex="-1" role="dialog" aria-labelledby="MyModel"
			aria-hidden="true">
			<div class="modal-dialog" style="width: 400px;">
				<div class="modal-content">
					<div class="modal-header">
						<button type="button" class="close" data-dismiss="modal" aria-hidder="true">&times;</button>
						<h4 class="modal-title" id="myModalLabel">message</h4>
						<input type="hidden" id="userID" />
					</div>
					<div class="modal-body form-inline">
						<div class="form-group">
							<label class="control-label" for="dotime">日期区间</label>
							<input style="width: 220px;" class="form-control" type="text" id="dotime" />
						</div>
						<br />
						<div class="form-group">
							<label class="control-label" for="IPAddress">IP地址</label>
							<input style="width: 220px;" class="form-control" type="text" id="IPAddress"
								required="required" />
						</div>
						<br />
						<br />
						<div class="modal-footer">
							<button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
							<button type="button" class="btn btn-primary" id="butSave">保存</button>
						</div>
					</div>
				</div>
			</div>
		</div>
	</body>
</html>
